package com.example.zyfypt_206shj.model;

import com.example.zyfypt_206shj.bean.SessionID;
import com.example.zyfypt_206shj.iface.IsShoucangListener;
import com.example.zyfypt_206shj.iface.IsShoucangiface;
import com.example.zyfypt_206shj.service.IsShoucangService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Created by Raindy on 2017/5/24.
 */

public class IsShoucangModel implements IsShoucangiface{

    private Retrofit retrofit;
    private String BASEURL="http://amicool.neusoft.edu.cn/";

    public IsShoucangModel() {
        retrofit=new Retrofit.Builder()
                .baseUrl(BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .addConverterFactory(ScalarsConverterFactory.create())
                .build();
    }

    @Override
    public void getResult(String mod, int resid, final IsShoucangListener listener) {
        IsShoucangService isShoucangService = retrofit.create(IsShoucangService.class);
        Call<String> call = isShoucangService.exists(mod,resid, SessionID.sessionID);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.isSuccessful() && response.body()!=null)
                    listener.onResponse(response.body());
                else
                    listener.onFail("on response fall");
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }
}
